<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 地区函数库
 * 
 * @desc 
 * @version 1.0.0 
 * @author  Jason
 * @date	2017年5月3日 下午5:37:18
 */
class Lib_area extends Lib_database
{
	/** @var Mod_area */
	private $mod_area = null;
	
	/** @var Lib_language */
	private $lib_language = null;
	
	use libraries\common\LanguageCURD;
	
	use libraries\common\ConditionParser;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->CI->load->model('mod_area');
		$this->mod_area =& $this->CI->mod_area;
		
		$this->CI->load->library('lib_language');
		$this->lib_language =& $this->CI->lib_language;
		
		$this->set_render_config();
	}
	
	private function set_render_config()
	{
		$foreign_keys = [
				'master' => [
						['parent_id', ['name', 'short_name'], $this->mod_area, 'master', 'id'],
				],
		];
		$time_fields = [
				'master' => [],
		];
		$encode_fields = [
				'master' => [],
		];
		$this->foreign_keys = $foreign_keys;
		$this->time_fields = $time_fields;
		$this->encode_fields = $encode_fields;
	}
	
	/**
	 * 获取子级地区列表
	 * 
	 * @desc    获取子级地区列表
	 * @method  GET
	 * @author  Jason
	 * @date    2017年5月3日 下午6:22:45
	 * @param   number $parent_id
	 * @param   string $language
	 * 
	 * @return  [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, <userData>]
	 * 				[false, <errorMesg>]
	 */
	public function list_children($parent_id, $language)
	{
		$children = $this->mod_area->get_children_by_id($parent_id);
		
		$module = $this->mod_area->get_multi_language_info('module', 'master');
		
		$children = $this->lib_language->get_merged_list_data($children, $module, $language);
		
		foreach ($children as $key => $row) {
			# $volume[$key]  = $row['name'];
			$volume[$key]  = @iconv('UTF-8', 'GBK', $row['name']);
			// Message: iconv(): Detected an illegal character in input string
			// On language in ['french' => 'Français', 'german' => 'Deutsch', 'spanish' => 'Español'];
		}
		array_multisort($volume, SORT_ASC, SORT_LOCALE_STRING, $children);
		
		return [true, $children];
	}
}